//
//  YHKForwardView.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/2.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit
import SnapKit

class YHKForwardView: UIView {
    ///边距
    let margin = 10
    //记录下约束
    var bottomConstaint: Constraint?
    
    
    //定义一个模型来接收数据
    var homeCleeViewModel:YHKHomeCellViewModel?{
        
        didSet{
            
            
            forwardLabel.text = homeCleeViewModel?.retweetStatusString
            
            
            //cell重用，清空约束
            bottomConstaint?.uninstall()
            
            //设置转发微博的图片是否显示，和重新设置约束
            if let pic_url = homeCleeViewModel?.statrsModel?.retweeted_status?.pic_urls where pic_url.count ?? 0 > 0{
                
                //有
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                  //传递图片的数据
                //定义一个临时的数据进行数据拼接
                var result = [YHKPictureViewModel]()
                
                for model in pic_url{
                    
                    let viewModel = YHKPictureViewModel()
                 
                    viewModel.model = model
                    
                    result.append(viewModel)
                    
                    
                }
                pictureView.pictureList = result
                
            }else{
                //没有
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstaint = make.bottom.equalTo(forwardLabel.snp_bottom).constraint
                })
            }
            
        }
        
        
    }
    
    
    

   //添加视图三部曲
    //第一部
   override init(frame: CGRect) {
        super.init(frame: frame)
    
    
      setUpView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二部：定义一个私有方法，来添加控件和设置约束
    private func setUpView(){
       
        //添加控件
        addSubview(forwardLabel)
        addSubview(pictureView)
        
        //约束
        forwardLabel.snp_makeConstraints { (make) -> Void in
            //左 右 上 下
            make.left.equalTo(self.snp_left).offset(margin)
            make.right.equalTo(self.snp_right).offset(margin)
            make.top.equalTo(self.snp_top)
//            make.bottom.equalTo(self.snp_bottom)
        }
        
        
        pictureView.snp_makeConstraints { (make) -> Void in
            //上 左 右 宽 高
            
            make.top.equalTo(forwardLabel.snp_bottom)
            make.left.equalTo(margin)
            //            make.right.equalTo(self.snp_right).offset(margin)
            
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //告诉self底部在哪里
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
        
        
    }
    
    
    //第三部：懒加载
    private lazy var forwardLabel:UILabel = {
    
    let label = UILabel()
        
        label.text = "转发微博内容"
        
        label.numberOfLines = 0

        label.font = UIFont.systemFontOfSize(15)
        
        return label
    
    
    }()
    
    
    private lazy var pictureView:YHKPictureView = YHKPictureView()

}
